package com.cd.university.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cd.university.common.RestResult;
import com.cd.university.controller.form.MessageIdForm;
import com.cd.university.service.MessageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈栋
 * @create 2021/11/24 14:23
 */
@RestController
@RequestMapping("/msg")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/send")
    @ApiOperation("发送消息（动态给用户）给用户")
    public RestResult sendMessage(@RequestParam Integer userId,
                                  @RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false,defaultValue = "6") Integer pageSize){
        return messageService.sendMessage(userId, pageNum, pageSize);
    }

    @PostMapping("/haveReadList")
    @ApiOperation("批量已读消息")
    public RestResult haveReadMessageIdList(@RequestBody JSONObject jsonObject) {
        JSONArray idList = jsonObject.getJSONArray("messageIdList");
        List<MessageIdForm> messageIdList = idList.toJavaList(MessageIdForm.class);
        return messageService.approveMessage(messageIdList);
    }

    @GetMapping("/haveReadUserId")
    @ApiOperation("一键已读一个用户的所有消息")
    public RestResult haveReadMessageUserId(@RequestParam(required = true) Integer userId) {
        return messageService.approveAllByUserId(userId);
    }

}
